package vn.edu.vnu.sort;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Algorithm extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_algorithm);
        
        TextView tv = (TextView) findViewById(R.id.textView2);
        String chosen = null;
        
        Intent localIntent = getIntent();
        Bundle myBundle = localIntent.getExtras();
        final int algrithmNumber = myBundle.getInt("alg");
        if (algrithmNumber == 1) {
        	chosen = "The Bubble sort is chosen";
        } else if (algrithmNumber == 2) {
        	chosen = "The Quick sort is chosen";
        } else {
        	chosen = "The Heap sort is chosen";
        } 
        tv.setText(chosen);
        
        Button btn = (Button) findViewById(R.id.button1);
        final EditText edt = (EditText) findViewById(R.id.editText1); 
        
        final String chosen1 = chosen;
        
        btn.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				int numberArray = 0;
				try {
					numberArray = Integer.parseInt(edt.getText().toString());
				} catch (NumberFormatException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if (0 < numberArray && numberArray < 11) {
					Intent intent = new Intent();
					intent.setClassName("vn.edu.vnu.sort", "vn.edu.vnu.sort.CreateArray");
					intent.putExtra("alg", algrithmNumber);
					intent.putExtra("text", chosen1);
					intent.putExtra("number", numberArray);
					startActivity(intent);
				} else {
					Toast toast= Toast.makeText(getApplicationContext(), 
							"Invalid number!", Toast.LENGTH_SHORT);  
							toast.setGravity(Gravity.CENTER|Gravity.CENTER_HORIZONTAL, 0, 0);
							toast.show();
				}
			}
		});	
    }
}
